package org.grill.fatwhacker.gui;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.grill.fatwhacker.data.DailyRecord;
import org.joda.time.LocalDate;

public class DayMiniView {
	private Composite composite;
	private boolean selected = false;
	
	private Label day;
	private Label used;
	private Label available;
	private Label exercise;
	private Label flexUsed;
	
	public DayMiniView(Composite parent) {
		composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 10;
		gridLayout.marginWidth = 3;
		gridLayout.numColumns = 2;
		gridLayout.verticalSpacing = 3;
		composite.setLayout(gridLayout);
		composite.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent arg0) {
				paint(arg0);
			}
		});
		
		day = new Label(composite, SWT.NONE);
		GridData dayData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		dayData.horizontalSpan = 2;
		FontData fontData = day.getFont().getFontData()[0];
		fontData.setStyle(SWT.BOLD);
		Font f = new Font(parent.getDisplay(), fontData);
		
		
		day.setFont(f);
		day.setLayoutData(dayData);
		
		used = new Label(composite, SWT.NONE);
		available = new Label(composite, SWT.NONE);
		flexUsed = new Label(composite, SWT.NONE);
		exercise = new Label(composite, SWT.NONE);
		
		Listener listener = new Listener() {
					public void handleEvent(Event arg0) {
						System.out.println("Mouse");
						
						if (!selected) {
							selected = true;
							for (Listener l : listeners) {
								l.handleEvent(new Event());
							}
						}
					}
				};
		day.addListener(SWT.MouseDown, listener);
		composite.addListener(SWT.MouseDown, listener);
		used.addListener(SWT.MouseDown, listener);
		available.addListener(SWT.MouseDown, listener);
		flexUsed.addListener(SWT.MouseDown, listener);
		exercise.addListener(SWT.MouseDown, listener);
	}
	
	private ArrayList<Listener> listeners = new ArrayList<Listener>();
	
	public void addSelectionListener(Listener l) {
		listeners.add(l);
	}
	
	public void render() {
		if (record == null) 
			return;
		
		Color bgColor = null;
		if (selected) {
			bgColor = composite.getDisplay().getSystemColor(SWT.COLOR_GRAY); 
		}
		
		
		day.setBackground(bgColor);
		used.setBackground(bgColor);
		available.setBackground(bgColor);
		flexUsed.setBackground(bgColor);
		exercise.setBackground(bgColor);
		
		LocalDate date = record.getDate();
		day.setText(date.dayOfWeek().getAsShortText() + " " + date.monthOfYear().getAsShortText() + " " + date.getDayOfMonth() + " " + date.year().getAsShortText());
//		float cumTotal = record.getCumTotal();
//		System.out.println("FLoat cumTotal: " + cumTotal + ": val : " + String.valueOf(cumTotal));
		used.setText("Used: " + String.valueOf(record.getFoodTotal()));
		used.pack();
		available.setText("Left: " + String.valueOf(record.getRemaining()));
		available.pack();
		flexUsed.setText("Flex Used: " + String.valueOf(record.getFlexpointsUsed()));
		flexUsed.pack();
		exercise.setText("Exercise: " + String.valueOf(record.getExercisePoints()));
		exercise.pack();
		
//		composite.pack();
		composite.redraw();
	}
	
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
	public boolean isSelected() {
		return selected;
	}
	
	public void setRecord(DailyRecord record) {
		this.record = record;
	}
	
	public DailyRecord getRecord() {
		return record;
	}
	
	private DailyRecord record;
	
	int paintState = 0;
	
	private class FooRGB {
		FooRGB(int x, int y, int z) {
			red = x; green =y; blue = z;
		}
		
		int red, green, blue;
	}
	
	private void paintIntermediate(PaintEvent e) {
		RGB start = new RGB(200,200,200);
		RGB end = new RGB(120, 180, 200);
		
		FooRGB diff = new FooRGB(end.red - start.red, end.green - start.green, end.blue-start.blue);
		
		
		Point size = composite.getSize();
		
		Color c;
		if (paintState == 20) 
			c = new Color(e.display, end);
		else {
			float mult = (float) (0.05*paintState);
			RGB dest = new RGB((int) (start.red + mult*diff.red) , (int)(start.green + mult*diff.green), (int) ( start.blue + mult*diff.green));
			c = new Color(e.display, dest);
		}

		e.gc.setForeground(c);
		e.gc.drawRoundRectangle(0, 0, size.x-1, size.y-1, 5, 5);
		e.gc.drawRoundRectangle(1, 1, size.x-3, size.y-3, 5, 5);
		e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_GRAY));
		e.gc.fillRoundRectangle(2, 2, size.x-4, size.y-4, 5, 5);

		c.dispose();

	}
	
	private void paint(PaintEvent e) {
		if (selected) {
			
//			System.out.println("Painting: " + paintState);
			paintIntermediate(e);
			if (paintState < 20) {
				paintState++;
				e.display.timerExec(25, new Runnable() {
				
					public void run() {
						composite.redraw();
//						composite.getDisplay().update();
					}
				
				});
			}
		} else paintState = 0;
	}
}
